package com.earnings.okhttputils.utils.car.ui;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.CarUrl;
import com.earnings.okhttputils.utils.bean.HomeCarBean;
import com.earnings.okhttputils.utils.bean.HomeCarHotData;
import com.earnings.okhttputils.utils.bean.HomeRecommen;
import com.earnings.okhttputils.utils.car.http.CarHttp;
import com.earnings.okhttputils.utils.car.http.GodHttpArrayCallback;
import com.earnings.okhttputils.utils.god.GodAdapter.GodArrayList;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.utils.CarImageLoader;
import com.youth.banner.Banner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 汽车
 * Created by Administrator on 2017/6/14 0014.
 */

public class HomeCarActivity extends GodLeftHandBaseActivity {
    private GodArrayList<HomeCarHotData> godArrayList;
    private GodBaseAdapter adapter;
    private Banner banner;
    private CarImageLoader carImageLoader;
    private GodBaseAdapter recommendadapter;
    private View headView;
    private ImageView imgeview1, imgeview2, imgeview3, imgeview4;
    private ImageView imgs[];

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_homecar;
    }

    @Override
    protected void init() {
        setTitle("汽车");
        godArrayList = new GodArrayList();
        initView();

    }

    private void initView() {
        monitorRecyclerView();
        setRecyclerViewLayoutManager(R.id.recyclerview, new GridLayoutManager(this, 2));
        adapter = new GodBaseAdapter<HomeCarHotData>(R.layout.item_homecar, godArrayList) {
            @Override
            public void onBind(GodViewHolder holder, int position, HomeCarHotData data) {
                Glide.with(HomeCarActivity.this).load(data.getThumb()).into((ImageView) holder.getView(R.id.img_view));
                holder.setText(R.id.name_tv, data.getTitle());
                holder.setText(R.id.money_tv, "￥" + data.getPrice() + "万");
                holder.setText(R.id.price_tv, "￥" + data.getGprice() + "万");
            }

            @Override
            public void OnItemClickListener(View view, int position, HomeCarHotData data) {
                String catid = data.getCatid();
                String id = data.getId();
                mIntent.putExtra("catid", catid);
                mIntent.putExtra("id", id);
                skipActivity(CarDetailsActivity.class);
            }
        };
        headView = LayoutInflater.from(this).inflate(R.layout.item_recommen_head, null, false);
        adapter.setHeaderView(headView);
        adapter.setHeaderViewLine(true);
        setRecyclerViewAdapter(R.id.recyclerview, adapter);
        banner = getView(R.id.banner_view);
        carImageLoader = new CarImageLoader();
        banner.setImageLoader(carImageLoader);
        imgeview1 = (ImageView) headView.findViewById(R.id.img_view1);
        imgeview2 = (ImageView) headView.findViewById(R.id.img_view2);
        imgeview3 = (ImageView) headView.findViewById(R.id.img_view3);
        imgeview4 = (ImageView) headView.findViewById(R.id.img_view4);
        imgeview1.setOnClickListener(this);
        imgeview2.setOnClickListener(this);
        imgeview3.setOnClickListener(this);
        imgeview4.setOnClickListener(this);
        imgs = new ImageView[]{imgeview1, imgeview2, imgeview3, imgeview4};
        //广告轮播
        advertisement();
        //推荐8个东西
        recommend();
        //四个东西
        fourad();
        //热卖车辆
        hotcar();

    }

    @Override
    public void onClick(View v) {
        HomeCarBean data = null;
        if (v == imgeview1) data = datas.get(0);
        if (v == imgeview2) data = datas.get(1);
        if (v == imgeview3) data = datas.get(2);
        if (v == imgeview4) data = datas.get(3);
        String catid = data.getCatid();
        String id = data.getId();
        mIntent.putExtra("catid", catid);
        mIntent.putExtra("id", id);
        skipActivity(CarDetailsActivity.class);
    }


    private void monitorRecyclerView() {
        RecyclerView recyclerView = getView(R.id.recyclerview);
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            //用来标记是否正在向最后一个滑动
            boolean isSlidingToLast = false;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                // 当不滚动时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //获取最后一个完全显示的ItemPosition
                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
                    int totalItemCount = manager.getItemCount();

                    // 判断是否滚动到底部，并且是向右滚动
                    if (lastVisibleItem == (totalItemCount - 1) && isSlidingToLast) {
                        //加载更多功能的代码
                        hotcar();
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //dx用来判断横向滑动方向，dy用来判断纵向滑动方向
                if (dy > 0) {
                    //大于0表示正在向右滚动
                    isSlidingToLast = true;
                } else {
                    //小于等于0表示停止或向左滚动
                    isSlidingToLast = false;
                }
            }
        });

    }

    private int page = 1;

    private void hotcar() {
        showProgress();
        Map params = new HashMap();
        params.put("catid", "6");
        params.put("elite", "0");
        params.put("hot", "0");
        params.put("pagesize", "10");
        params.put("page", page + "");
        CarHttp http = new CarHttp("car", params);
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(http).build().execute(new GodHttpArrayCallback<HomeCarHotData>() {
            @Override
            public void onSuccess(List<HomeCarHotData> datas, String msg, String response) {
                page++;
                godArrayList.addAll(datas);
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();
            }
        });

    }

    private void recommend() {
        GodArrayList godArrayList = new GodArrayList();
        String titles[] = new String[]{"本周热推", "违章查询", "推荐分享", "找车", "发现", "汽车保险"};
        int imgs[] = new int[]{R.mipmap.hot3x, R.mipmap.search_033x, R.mipmap.ornaments3x, R.mipmap.service3x, R.mipmap.insurance3x, R.mipmap.more3x};
        for (int i = 0; i < titles.length; i++) {
            HomeRecommen homerecommn = new HomeRecommen();
            homerecommn.setTitle(titles[i]);
            homerecommn.setImg(imgs[i]);
            godArrayList.add(homerecommn);
        }
        recommendadapter = new GodBaseAdapter<HomeRecommen>(R.layout.item_recommen, godArrayList) {
            @Override
            public void onBind(GodViewHolder holder, int position, HomeRecommen data) {
                holder.setText(R.id.title_tv, data.getTitle());
                holder.setImageResource(R.id.img_view, data.getImg());
            }

            @Override
            public void OnItemClickListener(View view, int position, HomeRecommen data) {
                switch (position) {
                    //本周热推
                    case 0:
                        skipActivity(DiscoverActivity.class);
                        //Toast.makeText(HomeCarActivity.this, "敬请期待", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(HomeCarActivity.this, "敬请期待", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(HomeCarActivity.this, "敬请期待", Toast.LENGTH_SHORT).show();
                        break;
                    //找车
                    case 3:
                        skipActivity(SeekCarActivity.class);
                        break;
                    //发现
                    case 4:
                        skipActivity(DiscoverActivity.class);
                        break;
                    case 5:
                        Toast.makeText(HomeCarActivity.this, "敬请期待", Toast.LENGTH_SHORT).show();
                        break;

                }


            }
        };

        setRecyclerViewLayoutManager(R.id.grid_view, new GridLayoutManager(this, 4)).setAdapter(recommendadapter);
    }

    private List<HomeCarBean> datas;

    private void fourad() {
        Map map = new HashMap();
        map.put("id", "13");
        CarHttp carHttp = new CarHttp("ad", map);
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(carHttp).build().execute(new GodHttpArrayCallback<HomeCarBean>() {
            @Override
            public void onSuccess(List<HomeCarBean> datas, String msg, String response) {
                HomeCarActivity.this.datas = datas;
                for (int i = 0; i < datas.size(); i++) {
                    Glide.with(HomeCarActivity.this).load(datas.get(i).getThumb()).into(imgs[i]);
                }

            }

            @Override
            public void onFail(int code, String msg) {

            }
        });
    }

    private void advertisement() {
        Map map = new HashMap();
        map.put("id", "12");
        CarHttp carHttp = new CarHttp("ad", map);
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(carHttp).build().execute(new GodHttpArrayCallback<HomeCarBean>() {
            @Override
            public void onSuccess(List<HomeCarBean> datas, String msg, String response) {
                carImageLoader.start(banner, datas);
            }

            @Override
            public void onFail(int code, String msg) {

            }
        });
    }


}
